//
//  FWURLQuery.m
//  DDDDDD
//
//  Created by 范文青 on 14-6-13.
//  Copyright (c) 2014年 fwq. All rights reserved.
//

#import "FWURLQuery.h"
@interface FWURLQuery()
@property (nonatomic,strong)NSDictionary *paramsDic;
@property (nonatomic,strong)NSString     *query;
@end

@implementation FWURLQuery
- (instancetype)initWithURL:(NSURL *)url
{
    self = [super init];
    if (self) {
        self.URL = url;
    }
    return self;
}
- (instancetype)initWithQuery:(NSString *)query
{
    self = [super init];
    if (self) {
        self.query = query;
        [self parse];
    }
    return self;
}
- (void)setURL:(NSURL *)URL
{
    _URL = URL;
    self.query = [_URL query];
    [self parse];
}
- (void)parse
{
    NSMutableDictionary  *params = [NSMutableDictionary dictionary];
    NSString  *query = _query;
    NSArray  *paramsArray = [query componentsSeparatedByString:@"&"];
    [paramsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSArray  *paramObject = [obj componentsSeparatedByString:@"="];
        if ([paramObject count] == 2) {
             [params setObject:[paramObject objectAtIndex:1] forKey:[paramObject objectAtIndex:0]];
        }
    }];
    self.paramsDic = params;
}
- (NSString *)get:(NSString *)key
{
    return [_paramsDic objectForKey:key];
}
@end
